home *** CD-ROM | disk | FTP | other *** search
/ Freelog 15 / FREELOG 15.ISO / WebMaster / Perl / PERL5106.ZIP / perl5 / Lib / finddepth.pl < prev    next >
Encoding:
Text File  |  1995-12-02  |  2.5 KB  |  108 lines

  1. # Usage:
  2. #    require "finddepth.pl";
  3. #
  4. #    &finddepth('/foo','/bar');
  5. #
  6. #    sub wanted { ... }
  7. #        where wanted does whatever you want.  $dir contains the
  8. #        current directory name, and $_ the current filename within
  9. #        that directory.  $name contains "$dir/$_".  You are cd'ed
  10. #        to $dir when the function is called.  The function may
  11. #        set $prune to prune the tree.
  12. #
  13. # This library is primarily for find2perl, which, when fed
  14. #
  15. #   find2perl / -name .nfs\* -mtime +7 -exec rm -f {} \; -o -fstype nfs -prune
  16. #
  17. # spits out something like this
  18. #
  19. #    sub wanted {
  20. #        /^\.nfs.*$/ &&
  21. #        (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
  22. #        int(-M _) > 7 &&
  23. #        unlink($_)
  24. #        ||
  25. #        ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
  26. #        $dev < 0 &&
  27. #        ($prune = 1);
  28. #    }
  29.  
  30. use Cwd;
  31.  
  32. sub finddepth {
  33.     chop($cwd = cwd);
  34.     foreach $topdir (@_) {
  35.     (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
  36.       || (warn("Can't stat $topdir: $!\n"), next);
  37.     if (-d _) {
  38.         if (chdir($topdir)) {
  39.         ($fixtopdir = $topdir) =~ s,/$,, ;
  40.         &finddepthdir($fixtopdir,$topnlink);
  41.         ($dir,$_) = ($fixtopdir,'.');
  42.         $name = $fixtopdir;
  43.         &wanted;
  44.         }
  45.         else {
  46.         warn "Can't cd to $topdir: $!\n";
  47.         }
  48.     }
  49.     else {
  50.         unless (($dir,$_) = $topdir =~ m#^(.*/)(.*)$#) {
  51.         ($dir,$_) = ('.', $topdir);
  52.         }
  53.         chdir $dir && &wanted;
  54.     }
  55.     chdir $cwd;
  56.     }
  57. }
  58.  
  59. sub finddepthdir {
  60.     local($dir,$nlink) = @_;
  61.     local($dev,$ino,$mode,$subcount);
  62.     local($name);
  63.  
  64.     # Get the list of files in the current directory.
  65.  
  66.     opendir(DIR,'.') || warn "Can't open $dir: $!\n";
  67.     local(@filenames) = readdir(DIR);
  68.     closedir(DIR);
  69.  
  70.     if ($nlink == 2) {        # This dir has no subdirectories.
  71.     for (@filenames) {
  72.         next if $_ eq '.';
  73.         next if $_ eq '..';
  74.         $name = "$dir/$_";
  75.         $nlink = 0;
  76.         &wanted;
  77.     }
  78.     }
  79.     else {                    # This dir has subdirectories.
  80.     $subcount = $nlink - 2;
  81.     for (@filenames) {
  82.         next if $_ eq '.';
  83.         next if $_ eq '..';
  84.         $nlink = $prune = 0;
  85.         $name = "$dir/$_";
  86.         if ($subcount > 0) {    # Seen all the subdirs?
  87.  
  88.         # Get link count and check for directoriness.
  89.  
  90.         ($dev,$ino,$mode,$nlink) = lstat($_) unless $nlink;
  91.         
  92.         if (-d _) {
  93.  
  94.             # It really is a directory, so do it recursively.
  95.  
  96.             if (!$prune && chdir $_) {
  97.             &finddepthdir($name,$nlink);
  98.             chdir '..';
  99.             }
  100.             --$subcount;
  101.         }
  102.         }
  103.         &wanted;
  104.     }
  105.     }
  106. }
  107. 1;
  108.